﻿using Autofac;
using AutofacContrib.CommonServiceLocator;
using F2Enum.Converters;
using F2Enum.Core;
using Microsoft.Practices.ServiceLocation;

namespace F2Enum.Library
{
    public static class BootStrapper
    {
        private static IContainer _container;

        public static void Init()
        {
            if(_container != null)
                return;
            var builder = new ContainerBuilder();
            builder.RegisterType<ConverterFactory>().As<IConverterFactory>().SingleInstance();
            builder.RegisterGeneric(typeof (NullableConverter<>)).As(typeof(IConverter<>)).SingleInstance();
            builder.RegisterAssemblyTypes(typeof (BootStrapper).Assembly).AsClosedTypesOf(typeof (IConverter<>)).
                AsImplementedInterfaces().SingleInstance();
            _container = builder.Build();
            
            ServiceLocator.SetLocatorProvider(() => new AutofacServiceLocator(_container));

        }
    }
}